package com.sv.utils;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

public class MybatisSqlSessionFactory {

	@Autowired
	@Qualifier("sqlSession")
	private static SqlSessionTemplate sqlSession;

	public static SqlSessionTemplate getSqlSession() {
		if (null == sqlSession) {
			WebApplicationContext wac = ContextLoader
					.getCurrentWebApplicationContext();
			sqlSession = wac.getBean("sqlSession", SqlSessionTemplate.class);
		}
		return sqlSession;
	}

	public void setSqlSession(SqlSessionTemplate sqlSession) {
		MybatisSqlSessionFactory.sqlSession = sqlSession;
	}
}
